package com.zpb.admin.controller;

import com.zpb.framework.domain.ApiResult;
import com.zpb.framework.domain.dto.CategoryAddDto;
import com.zpb.framework.domain.dto.CategoryQueryDto;
import com.zpb.framework.domain.dto.CategoryUpdateDto;
import com.zpb.framework.domain.entity.Category;
import com.zpb.framework.domain.vo.CategoryAdminVo;
import com.zpb.framework.domain.vo.CategoryVo;
import com.zpb.framework.domain.vo.PageVo;
import com.zpb.framework.service.CategoryService;
import com.zpb.framework.utils.BeanCopyUtil;
import com.zpb.framework.utils.ExcelUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2025/9/20
 */
@RestController
@RequestMapping("/content/category")
public class ContentCategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/listAllCategory")
    public ApiResult<List<CategoryVo>> listAllCategory() {
        return categoryService.getAll();
    }
    @PreAuthorize("@ps.hasAuthority('content:category:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response)  {

        List<Category> list = categoryService.getCategoryList();
        List<Category> categoryList = BeanCopyUtil.copyBeanList(list, Category.class);
        ExcelUtils<Category> excelUtils = new ExcelUtils<>();
        try {
            excelUtils.create(response,categoryList,Category.class,"分类列表");
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    @GetMapping("/list")
    public ApiResult<PageVo> getCategoryPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                             @RequestParam(defaultValue = "2") Integer pageSize,
                                             CategoryQueryDto categoryQueryDto){
        return categoryService.getCategoryPage(pageNum,pageSize,categoryQueryDto);
    }
    @PostMapping
    public ApiResult addCategory(CategoryAddDto categoryAddDto) {
        return categoryService.addCategory(categoryAddDto);
    }

    @GetMapping("/{id}")
    public ApiResult<CategoryAdminVo> getCategory(@PathVariable Long id) {
        return categoryService.getCategoryById(id);
    }
    @PutMapping
    public ApiResult updateCategory(@RequestBody CategoryUpdateDto categoryUpdateDto){
        return categoryService.updateCategory(categoryUpdateDto);
    }
    @DeleteMapping("/{id}")
    public ApiResult deleteCategory(@PathVariable Long id) {
        return categoryService.delCategoryById(id);
    }
}
